<template>
	<div class="account_frozen_wrapper">
		<div class="form">
			<h4>是否账号冻结</h4>
			<el-switch v-model="valiRule" active-value="1" inactive-value="2" @change="updateFrozenConfig"></el-switch>
		</div>
		<div class="tips">开启之后，输入几次错误后会进行登录锁定</div>
		<div class="set" v-if="valiRule === '1'">
			<sw-number-input v-model="frozenData.errorTimeLen" size="mini"></sw-number-input>
			<font>秒钟内，</font>
			<span>账号连续输错超过</span>
			<sw-number-input v-model="frozenData.errorCount" size="mini"></sw-number-input>
			<font>次，</font>
			<span>锁定账号</span>
			<sw-number-input v-model="frozenData.frozenLen" size="mini"></sw-number-input>
			<font>秒钟</font>
			<el-button size="mini" type="primary" @click="updateFrozenConfig">保存</el-button>
		</div>
		<div class="sw_table_container" v-loading="loading">
			<el-table size="medium" :data="frozenListData" stripe height="100%">
				<el-table-column prop="userName" label="用户名称"></el-table-column>
				<el-table-column prop="frozenTime" label="冻结开始时间"></el-table-column>
				<el-table-column prop="createName" label="冻结时长">
					<template slot-scope="{ row }">{{ row.frozenLen }}秒钟</template>
				</el-table-column>
				<el-table-column prop="content" label="当前状态">
					<template slot-scope="{ row }">
						<el-tag size="small" v-if="row.isFrozen === '1'" type="danger">锁定</el-tag>
						<el-tag size="small" v-if="row.isFrozen === '2'" type="success">正常</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="90">
					<template slot-scope="{ row }">
						<el-button size="medium" type="text" @click="handleRemoveFrozen(row)">{{ row.isFrozen === '1' ? '解除冻结' : '冻结账号' }}</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<sw-pagination :total="dataCount" :page.sync="queryData.page" :limit.sync="queryData.limit" @change="queryFrozenRecordData"></sw-pagination>
	</div>
</template>

<script>
import { mapGetters } from 'vuex';
import { updateFrozenSetting } from '@/api/platform/tbSysSite';
import { getFrozenRecordList, updateFrozenRecord } from '@/api/platform/tbFrozenRecord';
export default {
	data() {
		return {
			loading: false,
			valiRule: '',
			frozenData: {},
			queryData: { page: 1, limit: 15 },
			dataCount: 0,
			frozenListData: []
		};
	},
	computed: {
		...mapGetters(['siteInfo'])
	},
	mounted() {
		this.valiRule = this.siteInfo.valiRule;
		this.frozenData = {
			frozenLen: this.siteInfo.frozenLen,
			errorCount: this.siteInfo.errorCount,
			errorTimeLen: this.siteInfo.errorTimeLen
		};
		this.queryFrozenRecordData();
	},
	methods: {
		//更新锁定设置
		async updateFrozenConfig() {
			let params = { id: this.siteInfo.id, ...this.frozenData, valiRule: this.valiRule };
			let result = await updateFrozenSetting(params).then((res) => res.data);
			if (result.code === 0) {
				this.$message.success('修改冻结设置成功');
				this.$store.dispatch('getSiteBaseInfo');
			} else {
				this.$message.error('修改冻结设置失败');
			}
		},

		//获取冻结列表
		async getFrozenRecordData() {
			let params = { ...this.queryData };
			let result = await getFrozenRecordList(params).then((res) => res.data);
			this.loading = false;
			if (result.code === 0) {
				this.dataCount = parseInt(result.count);
				this.frozenListData = !result.data ? [] : result.data;
			}
		},

		//查询冻结数据
		queryFrozenRecordData() {
			this.loading = true;
			this.getFrozenRecordData();
		},

		//解除冻结数据
		handleRemoveFrozen(data) {
			let that = this;
			this.$confirm(`此操作将解除用户<font color="#f00">【${data.userName}】</font>的锁定, 是否继续?`, '删除提示', {
				type: 'warning',
				dangerouslyUseHTMLString: true,
				async beforeClose(action, instance, done) {
					if (action === 'confirm') {
						instance.confirmButtonLoading = true;
						await that.updateUserForzenStatus(data);
						instance.confirmButtonLoading = false;
						done();
					} else {
						done();
					}
				}
			}).catch(() => {});
		},

		//修改用户账号冻结状态
		async updateUserForzenStatus(data) {
			let frozenStatus = data.isFrozen === '1' ? '2' : '1';
			let params = { id: data.id, isFrozen: data.isFrozen === '1' ? '2' : '1' };
			let result = await updateFrozenRecord(params).then((res) => res.data);
			if (result.code === 0) {
				this.$message.success('解除用户冻结成功');
				data.isFrozen = frozenStatus;
			} else {
				this.$message.error('解除用户冻结失败');
			}
		}
	}
};
</script>

<style lang="scss" scoped>
.account_frozen_wrapper {
	display: flex;
	flex-direction: column;
	flex: 1;
	padding: 16px;
	overflow: hidden;
	.form {
		display: flex;
		align-items: center;
		h4 {
			margin-right: 10px;
			line-height: 1;
		}
	}
	.tips {
		font-size: 14px;
		line-height: 1;
		color: #909399;
		margin-top: 10px;
	}
	.set {
		display: flex;
		align-items: center;
		margin-top: 12px;
		span {
			margin-right: 10px;
		}
		font {
			margin-left: 10px;
		}
		.el-input__inner {
			padding: 0 10px;
		}
		.el-input {
			width: 60px;
			font-size: 14px;
			text-align: center;
		}
		.el-button {
			margin-left: 10px;
		}
	}
	.sw_table_container {
		margin-top: 16px;
		padding-top: 16px;
		border-top: solid 1px #ebeef5;
	}
}
</style>
